﻿/**
 * @author sminutoli
 * versión 1.0
 * manda los mensajes, se pega un validado básico
 */

import com.dinmax.flash.utilities.date.DateUtils
import com.dinmax.flash.utilities.textfield.TextFieldUtils

class com.dinmax.flash.central.bridge.MESSAGESenderBridge
extends MovieClip {
	
	private var mcNote:MovieClip;
	private var mcClose:MovieClip;
	private var mcSend:MovieClip;
	private var txt_date:TextField;
	private var txt_subject:TextField;
	private var txt_to:TextField;
	private var txt_body:TextField;
		
	private var listener:Object;
	private var value:Object;
	
	private var extTo:String;
	
	public function MESSAGESenderBridge() {
		//trace("new MESSAGESenderBridge()");
		var root:MESSAGESenderBridge = this;
		this.initField(txt_subject);
		this.initField(txt_to);
		this.initField(txt_body);
		mcNote.onLoad= function():Void {
			this.gotoAndStop("write");
		};
		mcSend.onPress=function():Void {
			root.validate();
		};
		
		mcClose.onPress= function():Void {
			root.listener.closeWindow();
		};
		
		txt_to.onLoad = function():Void {
			trace("MESSAGESenderBridge.as txt_to.onLoad extTo= "+root.extTo);
			if(root.extTo != undefined){
				this.text = root.extTo;
			};
		};
				
	}
	
	public function initField(field:TextField):Void {
		field.onSetFocus=function():Void {
			if(!this.userInput){
				this.userInput = true;
				this.text = "";
			};
		};
		return;
	};
		
	public function validate():Void {
		try {
			TextFieldUtils.validateFields(new Array(txt_to, txt_subject, txt_body));
			listener.sendMessage(Number(txt_to.text), txt_subject.text, txt_body.text);
			listener.closeWindow();
		} catch (e) {
			trace("MESSAGESenderBridge.as | validate() | ERROR: "+e);
		};
		return;
	};
	
	public function setSubject(subject:String):Void {
		txt_subject.text = subject;
		return;
	};
	
	public function setBody(body:String):Void {
		txt_body.text = body;
		return;
	};
	
	public function setDate(date:String):Void {
		txt_date.text = date;
		return;
	};
	
	public function setTo(extId:Number):Void {
		trace("MESSAGESenderBridge.as setTo() = "+extId);
		if(extId != null && extId != undefined && extId != "NaN"){
			this.extTo = String(extId);
			txt_to.text = String(extId);
		};
		return;
	};
	
	public function setListener(listener:Object):Void {
		this.listener = listener;
	}
	public function setValue(value:Object):Void {
		this.value = value;
	}
};